package com.sample.springbootsample.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @projectName: spring-boot-sample
 * @description:
 * @author: Adam
 * @create: 2019-12-19 19:17
 **/

@Controller
public class RabbitMqDemo {


    private Logger logger = LoggerFactory.getLogger(RabbitMqDemo.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @RequestMapping(value = "/rabbitmq",method = RequestMethod.GET)
    @ResponseBody
    public String send(){

        logger.info("进入rabbitMq");
        rabbitTemplate.convertAndSend("TestDirectExchange","TestDirectBinding","hello world!");
        return "rabbitmq";
    }

    @RequestMapping(value = "/rabbitmq2",method = RequestMethod.GET)
    @ResponseBody
    public String send2(){

        logger.info("进入rabbitMq2");
        rabbitTemplate.convertAndSend("TestDirectExchange","testRoutingKey","no binding routingKey!");
        return "rabbitmq";
    }

    /*@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "ORDER_PRODUCT_CHECK_QUEUE", durable = "true"),
            exchange = @Exchange(name = "order.direct.ex")), concurrency = "${rabbitmq.concurrency:10}",
            containerFactory = "rabbitListenerContainerFactory")*/
    @RabbitListener(queues = "TestDirectQueue")
    public void receive(@Payload String msg){
         logger.info("mq消费消息:"+msg);
    }

    //@RabbitListener(bindings = @QueueBinding(
    //            value = @Queue("fruitOrder"),
    //            key = "fruit",
    //            exchange = @Exchange("myOrder")
    //    ))
    // 还可以通过注解自动创建Queue，Exchange，RoutingKey和绑定关系
    @RabbitListener(queues = "testRoutingKey")
    public void receive2(@Payload String msg){
        logger.info("mq消费消息："+msg);
    }
}
